//
//  ByCellMemberStoreType.m
//  mijifen_ios
//
//  Created by mc on 2017/10/31.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellMemberStoreType.h"

// view
#import "ByViewMemberStoreMode.h"

@implementation ByCellMemberStoreType
{
    UILabel                 *_titleLabel;
    UIView                  *_rightView;
    UIImageView             *_arowImg;
    NSString                *_title;
    NSInteger               _section;
    UILabel                 *_contentLabel;
    ByViewMemberStoreMode   *_modeView;
}

+ (instancetype)cellWithTableView:(UITableView*)tableView andTitle:(NSString *)title andSection:(NSInteger)section{
    
    static NSString *cellIdentifier = @"ByCellMemberStoreType";
    ByCellMemberStoreType *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier andTitle:title andSection:section];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andTitle:(NSString *)title andSection:(NSInteger)section{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _title = title;
        _section = section;
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    _titleLabel = [[UILabel alloc] init];
    [self addSubview:_titleLabel];
    CGFloat width = [@"储值优惠信息" getSizeOfString:16].width + 30;
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top);
        make.size.mas_equalTo(CGSizeMake(width, 50));
    }];
    _titleLabel.text = _title;
    _titleLabel.font= FONT(16);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.backgroundColor = [UIColor whiteColor];
    _titleLabel.layer.cornerRadius = 4.0;
    _titleLabel.layer.borderColor = [UIColor colorWithWhite:0.90 alpha:1].CGColor;
    _titleLabel.layer.borderWidth = 1;
    
    _rightView = [[UIView alloc] init];
    [self addSubview:_rightView];
    [_rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel.mas_right).offset(10);
        make.top.equalTo(_titleLabel.mas_top);
        make.bottom.equalTo(self.mas_bottom);
        make.right.equalTo(self.mas_right).offset(-10);
    }];
    
    _arowImg = [[UIImageView alloc] init];
    [_rightView addSubview:_arowImg];
    [_arowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_rightView.mas_right).offset(-10);
        make.top.equalTo(_rightView.mas_top).offset(10);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    _arowImg.image = [UIImage imageNamed:@"Conjuration_arrow_down"];
    
    _contentLabel = [[UILabel alloc] init];
    [_rightView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_rightView.mas_top).offset(10);
        make.left.equalTo(_rightView.mas_left).offset(5);
        make.bottom.equalTo(_rightView.mas_bottom).offset(-10);
        make.right.equalTo(_arowImg.mas_left).offset(-5);
    }];
    _contentLabel.font = FONT(16);
    
    _rightView.backgroundColor = [UIColor whiteColor];
    _rightView.layer.cornerRadius = 4.0;
    _rightView.layer.borderColor = [UIColor colorWithWhite:0.90 alpha:1].CGColor;
    _rightView.layer.borderWidth = 1;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAction)];
    _rightView.userInteractionEnabled = YES;
    [_rightView addGestureRecognizer:tap];
}

- (void)clickAction{
//    if (self.clickBlock){
//        self.clickBlock();
//    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"isShowMemberStore" object:nil];
    _modeView = [[ByViewMemberStoreMode alloc] init];
    if (_section == 2) {
        _modeView.modeArray = @[@"微信",@"支付宝",@"银行卡",@"余额"];
    }else{
        _modeView.modeArray = @[@"按次消费",@"按月消费",@"按年消费"];
    }
    __weak typeof(_contentLabel) weakContent = _contentLabel;
    __weak typeof(self) weakSelf = self;
    _modeView.didSelectBlock = ^(NSString *str) {
        weakContent.text = str;
        if (weakSelf.consumeBlock) {
            weakSelf.consumeBlock(str);
        }
    };
    [_modeView show];
}

@end
